<template>
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo"></div>
            <el-menu active-text-color="#ffd04b" background-color="#232323"  text-color="#fff"
                router>
                <el-menu-item index="/business/goodsManager">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>商品管理</span>
                </el-menu-item>
                <el-menu-item index="/business/browsingHistory">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>商品被浏览记录</span>
                </el-menu-item>
                <el-menu-item index="/business/order">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>商品订单管理</span>
                </el-menu-item>
                <el-menu-item index="/business/salesReport">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>销售报表</span>
                </el-menu-item>
                <el-sub-menu >
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item index="/business/info">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                    <el-menu-item index="/business/changeAvatar">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>更换头像</span>
                    </el-menu-item>
                    <el-menu-item index="/business/changePassword">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>重置密码</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>用户：<strong>{{ businessInfoStore.businessInfo.name }}</strong></div>
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="businessInfoStore.businessInfo.image? businessInfoStore.businessInfo.image : avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="changeAvatar" :icon="Crop">更换头像</el-dropdown-item>
                            <el-dropdown-item command="changePassword" :icon="EditPen">重置密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>伪淘宝 ©2024 Created by Me</el-footer>
        </el-container>
    </el-container>
</template>

<script setup>
import {
    Management,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { businessInfoService } from '@/api/Business'
import { useTokenStore } from '@/store/token'
import { useBusinessInfoStore } from '@/store/businessInfo'
import { useRouter } from 'vue-router'
import { useLoginStore } from '@/store/login'
import { ElMessage,ElMessageBox } from 'element-plus'
const router = useRouter();
const businessInfoStore = useBusinessInfoStore();
const tokenStore = useTokenStore();
const loginStore = useLoginStore();
const getBusinessInfo = async() => {
    const result = await businessInfoService();
    businessInfoStore.setBusinessInfo(result.data);
}

getBusinessInfo();

//头像下的条目点击后调用的函数
const handleCommand = (command) => {
    if(command === 'logout') {
        //退出登录
        ElMessageBox.confirm(
            '您确认要退出吗?',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        ).then(async () => {
            //确认了退出登录
            //1.情况pinia中的token和个人信息
            tokenStore.removeToken();
            businessInfoStore.removeBusinessInfo();
            loginStore.removeLoginInfo();
            //2.跳转到登录页面
            router.push('/Login');
            ElMessage({
                type: 'success',
                message: '退出登录成功',
            })
        }).catch(() => {
            ElMessage({
                type: 'info',
                message: '用户取消了退出',
            })
        })
    }else {
        router.push('/business/'+command);
    }
}
</script>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #232323;

        &__logo {
            height: 120px;
            background: url('@/assets/logo.png') no-repeat center / 120px auto; //商标
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #00887ae8;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #be2929;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>